//
//  XSWelcomeViewController.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/6.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


class XSWelcomeViewController: UIViewController {

    
    
    override func viewDidLoad() {
        setupUI()
    }
    
    func setupUI(){
        
        view.backgroundColor = UIColor.whiteColor()
        
        
        view.addSubview(iconImage)
        view.addSubview(descLabel)
        
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(300)
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        descLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage)
            make.top.equalTo(iconImage.snp_bottom).offset(20)
        }

        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(150)
        }
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 动画执行完毕,执行label显示的动画
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.descLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        print("跳转到mainViewController")
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(XSChangeRootVCNotification, object: self)
                })
        }
    }
    
    private lazy var iconImage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 50
        imageView.layer.masksToBounds = true
        let iconImageUrlString = XSUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large
        imageView.sd_setImageWithURL(NSURL(string: iconImageUrlString!), placeholderImage: UIImage(named: "avatar_default_big"))
        return imageView
    }()
    
    // 欢迎回来的label
    private lazy var descLabel: UILabel = {
        let label = UILabel(textColor: UIColor.purpleColor(), fontSize: 18)
        label.text = "Welcome Back."
        label.sizeToFit()
        label.alpha = 0
        return label
    }()

}

